<template>
<div class="panel">
    <div class="panel-header">
        <el-button type="primary" @click="createSign">创建签到</el-button>
    </div>
    <div class="panel-body">
        <el-table :data="signList" border>
            <el-table-column prop="user.realname" label="签到人"></el-table-column>
            <el-table-column prop="create_time" label="签到时间"></el-table-column>
        </el-table>
    </div>
</div>
</template>

<script>
import {
    getSignListApi,
    createSignApi
} from '@/api/customer/sign.js'

export default {
    props: {
        allocate: {
            type: Object,
            default: function () {
                return {
                    id: '',
                    member_id: '',
                };
            }
        }
    },
    data() {
        return {
            signList: []
        };
    },
    methods: {
        createSign() {
            this.$confirm('确认签到吗？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                // User confirmed, proceed with sign-in
                createSignApi({
                    allocate_id: this.allocate.id,
                    member_id: this.allocate.member_id
                }).then(res => {
                    if (res.code == '200') {
                        // 提示创建成功
                        this.$message.success(res.msg);
                        this.fetchSignList();
                    } else {
                        this.$message.error(res.msg);
                    }
                });
            }).catch(() => {
                // User cancelled, do nothing
            });
        },
        fetchSignList() {
            // Call fetchSignListApi here and update signList
            getSignListApi({
                allocate_id: this.allocate.id
            }).then(res => {
                if (res.code == '200') {
                    this.signList = res.data.list;
                } else {
                    this.$message.error(res.msg);
                }
            }).catch(error => {
                console.error(error);
            });
        }
    }
};
</script>
